package com.shop.admin.controller;

import com.shop.Result.LdbzResult;
import com.shop.api.feign.*;
import com.shop.constant.Const;
import com.shop.utils.CookieUtils;
import com.shop.utils.FastJsonConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminLoginController {

    @Value("${sso.web.url}")
    private String SSO_WEB_URL ;

    @Autowired
    private AdminLoginFeign adminLoginFeign;

    @RequestMapping("/index")
    public String showIndex(Model model) {
        model.addAttribute("sso_web_url", SSO_WEB_URL);
        model.addAttribute("uid", UUID.randomUUID().toString());
        return "login";
    }

    @ResponseBody
    @RequestMapping("/login")
    public String userLogin(String account, String password,String code, String uid, HttpServletRequest request, HttpServletResponse response){
        LdbzResult ldbzResult = adminLoginFeign.userLogin(account, password, code, uid);
        if (ldbzResult.isOK())
            CookieUtils.setCookie(request,response, Const.TOKEN_LOGIN,ldbzResult.getData().toString());
        return FastJsonConvert.convertObjectToJSON(ldbzResult);
    }

    @RequestMapping("/info")
    public String showAdmin(Model model) {
        return "adminInfo";
    }

    @Autowired
    private AdminUserFeign adminUserFeign;
    @Autowired
    private AdminRoleFeign adminRoleFeign;
    @Autowired
    private AdminItemFeign adminItemFeign;
    @Autowired
    private AdminSheetFeign adminSheetFeign;
    @Autowired
    private AdminCategoryFeign adminCategoryFeign;
    @Autowired
    private AdminIndexSlidAdFeign adminIndexSlidAdFeign;

    @RequestMapping("/welcome")
    public String welcome(Model model){
        LdbzResult ldbzResult1 = adminUserFeign.countAdminUser(null);
        model.addAttribute("admin_users",ldbzResult1.getData());

        LdbzResult ldbzResult2 = adminRoleFeign.countAdminUser(null);
        model.addAttribute("admin_roles",ldbzResult2.getData());

        LdbzResult ldbzResult3 = adminCategoryFeign.countAdminUser(null);
        model.addAttribute("category",ldbzResult3.getData());

        LdbzResult ldbzResult4 = adminSheetFeign.countAdminUser(null);
        model.addAttribute("sheets",ldbzResult4.getData());

        LdbzResult ldbzResult5 = adminItemFeign.countAdminUser(null);
        model.addAttribute("items",ldbzResult5.getData());

        LdbzResult ldbzResult6 = adminIndexSlidAdFeign.countAdminUser(null);
        model.addAttribute("indexSlideAds",ldbzResult6.getData());
        return "adminWelcome";
    }
}
